<?php
/**
 * Created by : PhpStorm
 * User: ZPF
 * Date: 2022/4/23
 * Time: 21:48
 */

namespace app\admin\model;


use think\facade\Db;
use think\Model;

class BaseModel extends Model
{
    protected $model;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取全部列表
     * @param array $where
     * @param string $page
     * @param int $pageSize
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public static function getList($p = [])
    {
        $list = static::where($p['where']);
        if (!empty($p['with']))
            $list = $list->with($p['with']);
        if (!empty($p['field']))
            $list = $list->field($p['field']);
        if (!empty($p['withoutField']))
            $list = $list->withoutField($p['withoutField']);
        if (!empty($p['having']))
            $list = $list->having($p['having']);
        if (!empty($p['group']))
            $list = $list->group($p['group']);
        if (!empty($p['order']))
            $list = $list->order($p['order']);
        else
            $list = $list->order('id desc');
        if ($p['page'] != 'all')
            $list = $list->page($p['page'], $p['pageSize']);

        return $list->select();
    }

    /**
     * 按条件统计
     * @param array $where
     * @return mixed
     */
    public static function count($where = [])
    {
        return static::where($where)->count();
    }

    /**
     * @param array $p 编辑/新增
     */
    public static function edit($p = [])
    {
        $res = self::editWithReturn($p);
        api_return(($res) ? 200 : 500);
    }

    public static function editWithReturn($p = [])
    {
        if (!empty($p['id'])) {
            $res = static::update($p, ['id' => $p['id']]);
        } else {
            $res = static::create($p);
        }
        return $res;
    }

    /**
     * 详情
     * @param $where
     * @return mixed
     */
    public static function detail($where = [], $field = '*')
    {
        return static::where($where)->field($field)->find();
    }

    /**
     * 批量删除
     * @param $ids 逗号分割id字符串
     */
    public static function delByIds($ids)
    {
//        $res = static::where('id', 'in', $ids)->delete();
//        api_return(($res) ? 200 : 50001);
        Db::startTrans();
        $list = static::where('id', 'in', $ids)->select();
        foreach ($list as $item){
            $res = $item->delete();
            if(!$res){
                Db::rollback();
                api_return(50001);
            }
        }
        Db::commit();
        api_return(200);
    }

    /**
     * 查询当前字段内容是否重复
     * @param $field string 字段名
     * @param $data array 需要校验的数据，需要包含字段名以及主键id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function duplicateCheck($field, $data)
    {
        if (!empty($data['id'])) {
            $res = self::where([
                    ['id', '<>', $data['id']],
                    [$field, '=', $data[$field]]]
            )->find();
        } else {
            $res = self::where([[$field, '=', $data[$field]]])->find();
        }
        if ($res)
            api_return(500, $field . '重复' );
    }
}
